package com.ea.admin.controller.company;

import com.ea.admin.permission.PermissionCode;
import com.ea.admin.pojo.Enum.Permission;
import com.ea.admin.pojo.param.position.AddPositionParam;
import com.ea.admin.pojo.param.position.DeletePositionParam;
import com.ea.admin.pojo.param.position.EditPositionParam;
import com.ea.admin.pojo.param.position.GetPositionListParam;
import com.ea.admin.pojo.vo.position.GetPositionListVo;
import com.ea.admin.service.PositionService;
import com.ea.admin.utils.NowUserGetter;
import com.ea.common.log.LogAnnotation;
import com.ea.common.result.Result;
import com.ea.common.utils.ParamUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;

@RestController
@Api(tags = "单位管理-职位管理")
@RequestMapping("/admin/position")
public class PositionController {
    @Resource
    PositionService positionService;
    @Resource
    NowUserGetter nowUserGetter;

    @Transactional
    @Operation(summary = "获得所选单位的所有职位")
    @PostMapping("/getPositionList")
    @ApiImplicitParam(dataType = "GetPositionListParam", name = "getPositionListParam", value = "获得职位", readOnly = true, required = true)
    @LogAnnotation(module = "PositionController", operation = "获得职位")
    public Result<GetPositionListVo> getPositionList(@RequestBody @Valid GetPositionListParam getPositionListParam) {
        GetPositionListVo result = positionService.getPositionList(
                ParamUtil.parseIdList(getPositionListParam.getCompanyIdList()),
                getPositionListParam.getPositionName(),
                getPositionListParam.getPage(),
                getPositionListParam.getPageSize(),
                nowUserGetter.getNowUser().getAccountId()
        );
        return Result.success(result);
    }

    @Transactional
    @Operation(summary = "添加新的职位")
    @PostMapping("/addPosition")
    @ApiImplicitParam(dataType = "AddPositionParam", name = "addPositionParam", value = "添加新的职位", readOnly = true, required = true)
    @LogAnnotation(module = "PositionController", operation = "添加新职位")
    @Permission(permission_code = PermissionCode.ADD_POSITION,
            content = "为单位“{companyName}”添加新职位“positionName”")
    public Result<String> addPosition(@RequestBody @Valid AddPositionParam addPositionParam) {
        Long data=positionService.addPosition(
                addPositionParam.getPositionName(),
                ParamUtil.parseId(addPositionParam.getCompanyId()),
                nowUserGetter.getNowUser().getAccountId()
        );
        return Result.success(String.valueOf(data));
    }

    @Transactional
    @Operation(summary = "修改职位信息")
    @PostMapping("/editPosition")
    @ApiImplicitParam(dataType = "EditPositionParam", name = "editPositionParam", value = "修改职位信息", readOnly = true, required = true)
    @LogAnnotation(module = "PositionController", operation = "修改职位")
    @Permission(permission_code = PermissionCode.EDIT_POSITION,
            content = "修改单位“{companyName}”的职位“{positionName}”")
    public Result editPosition(@RequestBody @Valid EditPositionParam editPositionParam) {
        positionService.editPosition(
                ParamUtil.parseId(editPositionParam.getPositionId()),
                editPositionParam.getPositionName(),
                ParamUtil.parseId(editPositionParam.getCompanyId()),
                nowUserGetter.getNowUser().getAccountId()
        );
        return Result.success(null);
    }

    @Transactional
    @Operation(summary = "删除职位")
    @PostMapping("/deletePosition")
    @ApiImplicitParam(dataType = "DeletePositionParam", name = "deletePositionParam", value = "删除职位", readOnly = true, required = true)
    @LogAnnotation(module = "PositionController", operation = "删除职位")
    @Permission(permission_code = PermissionCode.DELETE_POSITION,
            content = "删除单位“{companyName}”的职位“{positionName}”")
    public Result deletePosition(@RequestBody @Valid DeletePositionParam deletePositionParam) {
        positionService.deletePosition(
                ParamUtil.parseId(deletePositionParam.getPositionId()),
                ParamUtil.parseId(deletePositionParam.getCompanyId()),
                nowUserGetter.getNowUser().getAccountId()
        );
        return Result.success(null);
    }
}
